/**
 * 作者: wgx
 * 日期: 2025-02-21 17:26:19
 * 备注: 玩家怪兽洞窟模块的被打记录
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.CaveManager;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.redis.RedisTools;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;

public class UserCaveRecord extends UserCaveRecordModel {
    public UserCaveRecord() {
        super();
    }

    public UserCaveRecord(long userId, long enemyId, String userName, int result, int areaType, int floorId, int floorIdNew) {
        super();
        putUserId(userId);
        putEnemyId(enemyId);
        putUserName(userName);
        putResult(result);
        putAddTime(TimeUtils.getCurTime());
        putAreaType(areaType);
        putFloorId(floorId);
        putFloorIdNew(floorIdNew);
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public JsonObject insert() {
        // 如果超过上限则会删除最早的那条
        long count = Future.await(RedisTools.getHashLen(getListKey()));
        if (count >= AllParam.CAVE_REPORT_NUM) {
            String orderListKey = EntityManager.getOrderListKey(getListKey());
            String delRecord = Future.await(RedisTools.lpop(orderListKey));// 删除列表数据
            long id = Long.parseLong(delRecord);// dbid
            UserCaveRecord record = CaveManager.getUserCaveRecorde(getUserId(), id);
            if (record != null) {
                record.delete();
            }
        }
        return super.insert();
    }
}
